package com.yohann.eduservice.controller.front;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yohann.commonutils.result.R;
import com.yohann.eduservice.entity.EduCourse;
import com.yohann.eduservice.entity.EduTeacher;
import com.yohann.eduservice.service.EduCourseService;
import com.yohann.eduservice.service.EduTeacherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

/**
 * <p>
 *
 * </p>
 *
 * @author Yohann
 * @since 2020/8/15
 */
@RestController
@RequestMapping("/edu-service/teacherFront")
public class TeacherFrontController {
    @Autowired
    private EduTeacherService teacherService;
    @Autowired
    private EduCourseService courseService;

    /**
     * 分页查询讲师
     * @param current
     * @param limit
     * @return
     */
    @GetMapping("pageQuery/{current}/{limit}")
    public R pageQueryTeacher(@PathVariable long current, @PathVariable long limit) {
        Page<EduTeacher> page = new Page<>(current, limit);

        QueryWrapper<EduTeacher> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("id");

        teacherService.page(page, wrapper);

        HashMap<String, Object> map = new HashMap<>();
        map.put("previous", page.hasPrevious());
        map.put("next", page.hasNext());
        map.put("total", page.getTotal());
        map.put("rows", page.getRecords());
        map.put("current", page.getCurrent());
        map.put("pages", page.getPages());
        map.put("size", page.getSize());

        return R.ok().data(map);
    }

    /**
     * 获取讲师信息以及课程
     * @param id
     * @return
     */
    @GetMapping("getTeacherInfo/{id}")
    public R getTeacherInfo(@PathVariable String id){
        EduTeacher teacher = teacherService.getById(id);

        QueryWrapper<EduCourse> wrapper = new QueryWrapper<>();
        wrapper.eq("teacher_id",id);
        List<EduCourse> courses = courseService.list(wrapper);

        return R.ok().data("teacher",teacher).data("courses",courses);
    }
}